﻿var com = com || {};
com.as = com.as || {};

com.as.Contract = function () {

};

com.as.Contract.prototype = {
    init: function () {
        // init wysiwyg
        this.initPluginWysiwyg();

        // init file upload
        this.initPluginFileUpload();

        // init placeholder
        this.initPluginPlaceHolder();

        // init data time picker
        this.initPluginDateTimePicker();

        // init wizard button
        this.initWizardButton();

        // init validate form
        this.setValidatorForm();
    },
    initPluginWysiwyg: function () {
        var opts = {
            cssClass: 'el-rte',
            height: 300,
            toolbar: 'normal',
            cssfiles: ['plugins/elrte/css/elrte-inner.css'],
            fmAllow: true,
            fmOpen: function (callback) {
                $('<div id="myelfinder"></div>').elfinder({
                    url: 'plugins/elfinder/connectors/php/connector.php',
                    lang: 'en',
                    height: 300,
                    dialog: { width: 640, modal: true, title: 'Select Image' },
                    closeOnEditorCallback: true,
                    editorCallback: callback
                });
            }
        }
        $('#elrte').elrte(opts);
    },
    initPluginFileUpload: function () {
        $("#elfinder").elfinder({
            url: 'plugins/elfinder/connectors/php/connector.php',
            lang: 'en',
            docked: true,
            height: 300
        });

        $("#uploader").pluploadQueue({
            // General settings
            runtimes: 'html4, html5',
            url: '../upload.html',
            max_file_size: '1000mb',
            max_file_count: 20, // user can add no more then 20 files at a time
            chunk_size: '1mb',
            unique_names: true,
            multiple_queues: true,

            // Resize images on clientside if we can
            resize: { width: 320, height: 240, quality: 90 },

            // Rename files by clicking on their titles
            rename: true,

            // Sort files
            sortable: true,

            // Specify what files to browse for
            filters: [
			    { title: "Image files", extensions: "jpg,gif,png" },
			    { title: "Zip files", extensions: "zip,avi" }
		    ]
        });
    },
    initPluginPlaceHolder: function () {
        $('input, textarea').placeholder();
    },
    initPluginDateTimePicker: function () {
        /* date and time Picker */
        $(".mws-datepicker").datepicker({ showOtherMonths: true });

        $(".mws-datepicker-wk").datepicker({ showOtherMonths: true, showWeek: true });

        $(".mws-datepicker-mm").datepicker({ showOtherMonths: true, numberOfMonths: 3 });

        $(".mws-dtpicker").datetimepicker();

        $(".mws-tpicker").timepicker({});
    },
    initWizardButton: function () {
        $("#wizard div.mws-button-row input[type=submit]").bind('click', function (event) {
            event.preventDefault();
            console.log($(this).parent().parent().attr('id'));
            if ($(this).parent().parent().attr('id') == "form-1" && $(this).val() == "Next") {
                $("#step1").addClass('clear');
                $("#step2").removeClass('clear');
                $("div.mws-wizard li:eq(0)").removeClass("current");
                $("div.mws-wizard li:eq(1)").addClass("current");
            }

            if ($(this).parent().parent().attr('id') == "form-2" && $(this).val() == "Next") {
                $("#step2").addClass('clear');
                $("#step3").removeClass('clear');
                $("div.mws-wizard li:eq(1)").removeClass("current");
                $("div.mws-wizard li:eq(2)").addClass("current");
            }

            if ($(this).parent().parent().attr('id') == "form-3" && $(this).val() == "Prev") {
                $("#step3").addClass('clear');
                $("#step2").removeClass('clear');
                $("div.mws-wizard li:eq(2)").removeClass("current");
                $("div.mws-wizard li:eq(1)").addClass("current");
            }

            if ($(this).parent().parent().attr('id') == "form-2" && $(this).val() == "Prev") {
                $("#step2").addClass('clear');
                $("#step1").removeClass('clear');
                $("div.mws-wizard li:eq(1)").removeClass("current");
                $("div.mws-wizard li:eq(0)").addClass("current");
            }
        });
    },
    /**
    * Validate  form
    * @param selector
    */
    setValidatorForm: function (selector) {
        validator = $("#mws-validate").validate({
            rules: {
                spinner: {
                    required: true,
                    max: 5
                }
            },
            invalidHandler: function (form, validator) {
                var errors = validator.numberOfInvalids();
                if (errors) {
                    var message = errors == 1
				? 'You missed 1 field. It has been highlighted'
				: 'You missed ' + errors + ' fields. They have been highlighted';
                    $("#mws-validate-error").html(message).show();
                } else {
                    $("#mws-validate-error").hide();
                }
            }
        });
    }
}